// pages/setting/setting.js
const app = getApp()
const G = app.globalData
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		netgates: [],
		netgate: 0,
		plants: [],
		t_plant: 0,  // 备选植物在数组中的序号
		t_subtype: 0,  // 备选植物子类在数组中的序号
		plant: -1,  // 已选植物在数组中的序号
		subtype: -1, // 已选植物子类在数组中的序号
		plant_start_date: '',  // 已选日期
		t_plant_start_date: '2020-01-01', // 备选日期
		chooseMenu: 0  ,// 0 显示选择植物，  1 显示设置设备
		
		renaming_gate: false, // 正在修改网关名
		renaming_switchs: [], // 正在修改某开关名
	},
	
	choosemenu: function(e){
		let i = e.currentTarget.dataset.i
		this.setData({
			chooseMenu: i
		})
	},
	
	bindPickerChange: function(e) {
		let i = e.detail.value
		this.setData({
			t_plant: i
		})
	},
	
	bindPickerChange2: function(e) {
		console.log(e.detail.value)
		let i = e.detail.value
		this.setData({
			t_subtype: i
		})
	},
	
	bindDateChange: function (e) {
		console.log('picker发送选择改变，携带值为', e.detail.value)
		this.setData({
			t_plant_start_date: e.detail.value
		})
	},
	
	renamegate: function(e){
		wx.navigateTo({
			url: "renamegate?id=" + this.data.netgates[this.data.netgate].id 
				+ "&name=" + this.data.netgates[this.data.netgate].comment_name
		})
	},
	
	renameswitch: function(e){
		// 传参，id和name
		let id = e.currentTarget.dataset.id
		let name = e.currentTarget.dataset.name
		wx.navigateTo({
			url: "renameswitch?netgate=" + this.data.netgate + "&id=" + id + "&name=" + name
		})
	},
	
	
	
	changeplant: function(){
		let that = this
		let param = {
			token: G.detail.token,
			id: this.data.netgates[this.data.netgate].id,
			plant_id: this.data.plants[this.data.t_plant].id,
			plant_type_id: this.data.plants[this.data.t_plant].types[this.data.t_subtype].id,
			plant_start_date: this.data.t_plant_start_date
		}
		app.get(app.urls.setgateplant, param, res => {
			console.log('setgateplant => ', res)
			that.setData({
				plant: that.data.t_plant,
				subtype: that.data.t_subtype,
				plant_start_date: that.data.t_plant_start_date
			})
		})
	},
	
	clearplant: function(){
		console.log('clearplant')
		let that = this
		let id = this.data.netgates[this.data.netgate].id
		console.log(id)
		app.get(app.urls.cleargateplant, {token: G.detail.token, id: id}, res => {
			console.log('cleargateplant => ', res)
			that.setData({
				plant: -1,
				t_plant: 0,
				t_subtype: 0,
				t_plant_start_date: '2020-01-01'
			})
		})
	},
	
	

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		let that = this
		// 接收传参，指定当前网关
		this.setData({
			netgate: options.netgate,
			netgates: G.datas.netgates,
		})
		
		// 获取植物名称
		app.get(app.urls.querygateplant, {token: G.detail.token, id: G.datas.netgates[options.netgate].id}, res => {
			console.log("querygateplant => ", res)
			
			// 通过id和subid 求当前农作物的index
			let plant = -1
			let subtype = -1
			for(let i in res.data.plants){
				let x = res.data.plants[i]
				if(x.id == res.data.plant_id){
					plant = i
					for(let j in x.types){
						let y = x.types[j]
						if(y.id == res.data.plant_type_id){
							subtype = j
						}
					}
					
				}
			}
			that.setData({
				plants: res.data.plants,
				plant: plant,
				subtype: subtype,
				plant_start_date: res.data.plant_start_date,
				
			})
		})
		
		
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {
	
		this.setData({
			netgates: G.datas.netgates,
		})
	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {
		let that = this
		if (G.datas.netgates.length > 0){
			this.setData({
				netgates: G.datas.netgates,
				sensor_data: G.datas.netgates[that.data.netgate].sensor_data,
				switchs: G.datas.netgates[that.data.netgate].switchs
			})	
		}
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})